﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Home : System.Web.UI.Page
{
    String strCon = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
    String strCom;
    SqlConnection objCon;
    SqlCommand objCom;
    SqlDataReader dtReader;
    SqlDataAdapter dtAdapter;
    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
            DeleteData(Convert.ToInt32(Request.QueryString["id"]));
        else
        {
            if (Session["strUser"] == null)
                Response.Redirect("Login.aspx");
            else
                lblWelcome.Text = "Welcome" + Session["strUser"] + DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss");
            BindData();
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Label lblWName = (Label)(e.Row.FindControl("lblWName"));
        if (lblWName != null)
            lblWName.Text = DataBinder.Eval(e.Row.DataItem, "WName").ToString();

        Label lblWOwner = (Label)(e.Row.FindControl("lblWOwner"));
        if (lblWOwner != null)
            lblWOwner.Text = DataBinder.Eval(e.Row.DataItem, "WOwner").ToString();

        LinkButton LinkButton1 = (LinkButton)(e.Row.FindControl("LinkButton1"));
        if (LinkButton1 != null)
            LinkButton1.PostBackUrl = "Detail.aspx?id=" + DataBinder.Eval(e.Row.DataItem, "WID").ToString();

        LinkButton LinkButton2 = (LinkButton)(e.Row.FindControl("LinkButton2"));
        if (LinkButton2 != null)
            LinkButton2.PostBackUrl = "Home.aspx?id=" + DataBinder.Eval(e.Row.DataItem, "WID").ToString();

    }
    protected void BindData()
    {
        objCon = new SqlConnection(strCon);
        objCon.Open();
        //CountAll
        strCom = "Select Count(*) From Webboards";
        objCom = new SqlCommand(strCom, objCon);

        dtReader = objCom.ExecuteReader();

        while (dtReader.Read())
        {
            lblCountAll.Text = dtReader[0].ToString();
        }

        dtReader.Close();
        dtReader = null;

        //CountMe
        strCom = "Select Count(*) From Webboards WHERE WOwner='" + Session["strUserID"] + "'";
        objCom = new SqlCommand(strCom, objCon);

        dtReader = objCom.ExecuteReader();

        while (dtReader.Read())
        {
            lblCountMe.Text = dtReader[0].ToString();
        }

        dtReader.Close();
        dtReader = null;

        //Grid
        strCom = "Select WID,WName,WOwner From Webboards Order by WID Desc";
        objCom = new SqlCommand(strCom, objCon);

        dtReader = objCom.ExecuteReader();
        GridView1.DataSource = dtReader;
        GridView1.DataBind();

        dtReader.Close();
        dtReader = null;
    }
    protected void DeleteData(int id)
    {
        objCon = new SqlConnection(strCon);
        objCon.Open();
        strCom = "Delete From Webboards Where WID = " + id +
            ";Delete From WebboardsDetail Where WID = " + id;
        objCom = new SqlCommand(strCom, objCon);
        objCom.ExecuteNonQuery();

        Response.Redirect("Home.aspx");
    }
}